package cn.shangjia.shop.controller.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasig.cas.client.facade.SecurityContext;

import cn.cnnic.cp.modules.soa.customer.entity.CustomerDTO;
import cn.shangjia.common.cache.memcache.CacheClient;
import cn.shangjia.common.domain.UserInfo;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.common.web.session.KnetSession;
import cn.shangjia.common.web.session.SessionManager;
import cn.shangjia.shop.config.ShopConfig;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.UserInfoService;
/**
 * Controller层基类.
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @author <a href="mailto:liangchuan@knet.cn">liangchuan</a>
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-01 12:00:12
 * @since 1.0
 */
public abstract class AbstractController {
	
	protected final Logger logger = ShopLoggerFactory.getLogger(getClass());
	
	@Resource
	private CacheClient sessionCacheClient;
	
	@Resource(name = "shopConfig")
	protected ShopConfig config;
	
	@Resource
	protected UserInfoService userInfoService;
	
	
	private UserInfo getCurrentUser() {
		CustomerDTO customer = SecurityContext.getCurrentLoginUser();
		if (customer == null) {
			return null;
		}
		UserInfo userInfo = userInfoService.getUserInfo(customer.getLoginId());
		return userInfo;
	}
	
	public UserInfo getCurrentUser(HttpServletRequest request,HttpServletResponse response) {
		KnetSession session =
				StringUtils.isEmpty(config.getSessionDomain())
					? getSession(request, response) 
					: getSession(request, response, config.getSessionDomain()); 
		if (session == null) {
			return null;
		}
		// 获取用户信息
		UserInfo userInfo = session.get(session.getSessionId());
		if (userInfo != null) {
			return userInfo;
		}
		userInfo = getCurrentUser();
		if (userInfo != null) {
//			session = StringUtils.isEmpty(config.getSessionDomain()) 
//								? SessionManager.getSession(request, response, cacheClient, true) 
//								: SessionManager.getSession(request, response, cacheClient, config.getSessionDomain(), true) ;
			session.put(session.getSessionId(), userInfo);
		}
		return userInfo;
	}
	
	
	/**
	 *  获取 knetSession
	 * 
	 * @param request
	 * @param response
	 * @param domain
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected KnetSession getSession(HttpServletRequest request, HttpServletResponse response, String domain) {
		try {
			KnetSession session = SessionManager.getSession(request, response, sessionCacheClient, domain,false);
			return session;
		} catch(Exception e) {
			logger.errorPair(e, "缓存服务器异常!", "domain", domain);
			return null;
		}
	}
	
	/**
	 *  获取 shangjia.cn 域下的 knetSession
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected KnetSession getSession(HttpServletRequest request, HttpServletResponse response) {
		return SessionManager.getSession(request, response, sessionCacheClient);
	}
	
	
	protected int getInt(String value) {
		return getInt(value, 1);
	}
	
	/**
	 *  将string型转int 主要用于 分页参数
	 * 
	 * @param value
	 * @param defaultValue
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected int getInt(String value, int defaultValue) {
		if (StringUtils.isEmpty(value)) 
			return defaultValue;
		try {
			return Integer.parseInt(value);
		} catch(Exception e) {
			return defaultValue;
		}
	}
	
}
